package planninglanguage.goals;

import org.jdom.Element;

import planninglanguage.conditions.Condition;
import planninglanguage.conditions.UnitKilledCondition;
import planninglanguage.softconditions.SoftCondition;
import planninglanguage.softconditions.UnitKilledSoftCondition;
import util.XMLWriter;
import base.WargusGameState;

public class KillUnitGoal extends Goal implements Cloneable{
	
	int m_unitID;
	
	public KillUnitGoal(int a_unitID)
	{
		m_unitID = a_unitID;
	}
	
	public int unitID()
	{
		return m_unitID;
	}
	
	public Object clone()
	{
		return new KillUnitGoal(m_unitID);
	}
	
	public Condition generateSuccessCondition(WargusGameState a_gamestate)
	{
		return new UnitKilledCondition(m_unitID);
	}
	
	public SoftCondition generateSuccessSoftCondition(WargusGameState a_gamestate)
	{
		return new UnitKilledSoftCondition(m_unitID);
	}	
	
	public float distance(Goal a_g) {
		if (!(a_g instanceof KillUnitGoal)) return 1;
		
		KillUnitGoal g = (KillUnitGoal) a_g;
		float d_u = 1;

		if (m_unitID == g.m_unitID) d_u = 0;
			
		return d_u*0.5f;
	}
	public void writeXML(XMLWriter w) {
		w.tag(Goal.openTag + " " + Goal.XMLtype + "=\"" + 
				this.getClass().getSimpleName() + "\"");
		{
			w.tag("unit-id", m_unitID);
		}
		w.tag(Goal.closeTag);
	}	
	protected static KillUnitGoal loadFromXML(Element e) {
		int unitID = Integer.parseInt(e.getChildText("unit-id"));
		return new KillUnitGoal(unitID);
	}
}
